package com.cyy.activitylifecycleyanalysisdemo.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.cyy.activitylifecycleyanalysisdemo.R;
import com.cyy.activitylifecycleyanalysisdemo.fragment.AbstractBaseFragment;
import com.cyy.activitylifecycleyanalysisdemo.fragment.DefaultFragment;
import com.cyy.activitylifecycleyanalysisdemo.utils.LogUtils;

public class HasFragmentActivity extends AbstractBaseActivity {

    AbstractBaseFragment fragment1;
    AbstractBaseFragment fragment2;

    Button btn_1;
    Button btn_2;

    FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_has_fragment);
        fragment1 = new DefaultFragment("Fragment_1");
        fragment2 = new DefaultFragment("Fragment_2");
        btn_1 = findViewById(R.id.btn_1);
        btn_2 = findViewById(R.id.btn_2);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);

        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fragment_contianer, fragment2);
        ft.add(R.id.fragment_contianer, fragment1);
        ft.hide(fragment2);
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        Log.d(getLogTag(), "onResumeFragments");
        if(ft != null){
            ft.commit();
            ft = null;
        }
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        Log.d(getLogTag(), "onAttachFragment");
    }

    @Override
    protected int getContentLayoutResId() {
        return -1;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_1:{
                FragmentManager fm  = getSupportFragmentManager();
                if(fragment1.isHidden()){
                    fm.beginTransaction().hide(fragment2).show(fragment1).commit();
                }
            } break;
            case R.id.btn_2:{
                FragmentManager fm  = getSupportFragmentManager();
                if(fragment2.isHidden()){
                    fm.beginTransaction().hide(fragment1).show(fragment2).commit();

                }
            } break;
            default: super.onClick(v);
        }
    }

    @Override
    protected String getLogTag() {
        return LogUtils.getClassTag(getClass());
    }



}
